package com.seis_645.appTesting;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CarCompareActivity extends Activity {

	TableLayout table;
	CarDataSource datasource;
	Spinner spinner1, spinner2;
	Car car1, car2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				R.layout.compare_car, null);
		this.setContentView(viewToLoad);
		spinner1 = (Spinner) findViewById(R.id.spinner_car1);
		spinner2 = (Spinner) findViewById(R.id.spinner_car2);
		datasource = new CarDataSource(this);
		datasource.open();
		List<Car> values = datasource.get_all_cars();
		ArrayAdapter<Car> adapter = new ArrayAdapter<Car>(this,
				android.R.layout.simple_spinner_item, values);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter);
		spinner2.setAdapter(adapter);

		table = (TableLayout) findViewById(R.id.updateTable);
		Button update = (Button) findViewById(R.id.btn_update_table);
		Button back = (Button) findViewById(R.id.btn_back);
		OnTouchListener updateListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					updateTable();
					// return true;
				}
				return false;
			}
		};

		OnTouchListener backListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					datasource.close();
					finish();
				}
				return false;
			}
		};

		update.setOnTouchListener(updateListener);
		back.setOnTouchListener(backListener);
	}

	private void updateTable() {

		car1 = (Car) spinner1.getSelectedItem();
		car2 = (Car) spinner2.getSelectedItem();
		System.out.println(car2.get_model() + " " + car1.get_model());

		for (int i = 1; i < table.getChildCount(); i++) {
			if (i < 9) {
				if (table.getChildAt(i).getId() != -1) {
					TableRow row = (TableRow) findViewById(table.getChildAt(i)
							.getId());
					int val1 = 0;
					int val2 = 0;
					switch (i) {
					case 1:
						val1 = car1.get_gmHwy();
						val2 = car2.get_gmHwy();
						break;
					case 2:
						val1 = car1.get_gmCity();
						val2 = car2.get_gmCity();
						break;
					case 3:
						val1 = car1.get_driveablility();
						val2 = car2.get_driveablility();
						break;
					case 4:
						val1 = car1.get_comfort();
						val2 = car2.get_comfort();
						break;
					case 5:
						val1 = car1.get_apperance();
						val2 = car2.get_apperance();
						break;
					case 6:
						val1 = car1.get_accessories();
						val2 = car2.get_accessories();
						break;
					case 7:
						val1 = car1.get_safety();
						val2 = car2.get_safety();
						break;
					case 8:
						val1 = (int)car1.get_price();
						val2 = (int)car2.get_price();
						break;
					}
					TextView col1 = (TextView) row.getChildAt(1);
					TextView col2 = (TextView) row.getChildAt(2);
					TextView col3 = (TextView) row.getChildAt(3);
					col1.setText(Integer.toString(val1));
					col2.setText(Integer.toString(val2));
					col3.setText(Integer.toString(val1 - val2));
				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		datasource.close();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

}
